package com.z100h.supplychain.evidence.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONArray;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.z100h.supplychain.core.EvidenceType;
import com.z100h.supplychain.evidence.entity.WarehouseReceipt;
import com.z100h.supplychain.evidence.entity.WarehouseReceiptVO;
import com.z100h.supplychain.evidence.facade.WarehousingFacade;
import com.z100h.supplychain.purchase.entity.PurchaseOrderItemActualVo;
import com.z100h.supplychain.purchase.entity.PurchaseOrderItemVo;
import com.z100h.supplychain.purchase.service.PurchaseOrderItemActualService;
import com.z100h.supplychain.purchase.service.PurchaseOrderItemService;
import com.z100h.supplychain.sku.entity.Sku;
import com.z100h.supplychain.sku.service.SkuService;

/**
 * 入库单
 */
@Controller
@RequestMapping(value = "${adminPath}/warehouse/warehouseReceipt")
public class WarehouseReceiptController extends BaseController {

	@Autowired
	private WarehousingFacade<WarehouseReceipt> warehousingFacade;

	@Autowired
	private SkuService skuService;

	@Autowired
	private PurchaseOrderItemService purchaseOrderItemService;

	@Autowired
	private PurchaseOrderItemActualService purchaseOrderItemActualService;

	/**
	 * 列表页面
	 *
	 * @return
	 */
	@RequiresPermissions("warehouse:warehouseReceipt:list")
	@RequestMapping(value = "")
	public String list() {
		return "supplychain/warehouseReceipt/list";
	}

	/**
	 * 列表数据页面
	 *
	 * @param page
	 * @param warehouseReceipt
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("warehouse:warehouseReceipt:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(Page<WarehouseReceipt> page, WarehouseReceipt warehouseReceipt,
			HttpServletRequest request, HttpServletResponse response) {
		page = warehousingFacade.findWarehouseReceiptByPage(page, warehouseReceipt);
		return getBootstrapData(page);
	}

	@RequiresPermissions("warehouse:warehouseReceipt:add")
	@RequestMapping(value = "addWarehouseReceipt")
	public String addWarehouseReceipt(HttpServletRequest request) {

		// 查询同仓库下的sku
		Sku sku = new Sku();
		sku.setDepartmentId(UserUtils.getUser().getOffice().getId());
		List<Sku> skuList = skuService.findList(sku);
		request.setAttribute("skuList", skuList);

		return "supplychain/warehouseReceipt/add_warehouseReceipt";
	}

	@ResponseBody
	@RequiresPermissions("warehouse:warehouseReceipt:edit")
	@RequestMapping(value = "save")
	public Map<String, Object> save(Page<WarehouseReceipt> page, WarehouseReceipt warehouseReceipt,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			warehousingFacade.save(warehouseReceipt);
			map.put("message", 0);
		} catch (Throwable e) {
			logger.error("save is error.", e);
			map.put("message", 1);
			map.put("data", e.getMessage());// 把异常信息带到前台
		}
		return map;
	}

	/**
	 * 
	 * 质检入库
	 * 
	 * @param page
	 * @param warehouseReceipt
	 * @param request
	 * @param response
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月18日 下午2:43:06
	 */
	@ResponseBody
	@RequestMapping(value = "saveAndQc")
	public Map<String, Object> saveAndQc(Page<WarehouseReceipt> page, WarehouseReceipt warehouseReceipt,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			warehousingFacade.saveAndQc(warehouseReceipt);
			map.put("message", 0);
		} catch (Throwable e) {
			logger.error("save is error.", e);
			map.put("message", 1);
			map.put("data", e.getMessage());// 把异常信息带到前台
		}
		return map;
	}

	/**
	 * Request URL: http://192.168.199.199:8080/zbh-sc/a/warehouse/warehouseReceipt/saveAndQcFromMp
	
	 * 
	 * evidenceId: 427
	 * warehouseId: 57
	 * josns选填
	 * jsons: [{"skuId":"1677","price":"1","num":"11"},{"skuId":"1676","price":"2","num":"1"}]
	 * remarks: 123123
	 * 
	 * @param warehouseReceipt
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2020年4月21日 下午3:20:58
	 */
	@ResponseBody
	@RequestMapping(value = "mp/qc")
	public AjaxJson saveAndQcFromMp(HttpServletRequest request, WarehouseReceipt warehouseReceipt) {

		AjaxJson respBody = new AjaxJson();
		try {
			if (localLogin(request)) {
				warehouseReceipt.setJsons(null);
				warehousingFacade.saveAndQc(warehouseReceipt);
			}
		} catch (Throwable e) {
			respBody.setSuccess(false);
			respBody.setMsg("入库单质检异常：" + e.getMessage());
			logger.error("saveAndQcFromMp is error.", e);
		}
		return respBody;
	}

	/**
	 * 导出excel文件
	 */
	@ResponseBody
	@RequiresPermissions("warehouse:warehouseReceipt:export")
	@RequestMapping(value = "export", method = RequestMethod.GET)
	public AjaxJson exportFile(WarehouseReceipt warehouseReceipt, HttpServletRequest request,
			HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
			String fileName = "入库打码" + DateUtils.getDate("yyyyMMddHHmmss") + ".xls";
			List<WarehouseReceiptVO> list = warehousingFacade.findWarehouseReceiptToResourceByExcel(warehouseReceipt);
			new ExportExcel("入库打码", WarehouseReceiptVO.class).setDataList(list).write(response, fileName).dispose();
			j.setSuccess(true);
			j.setMsg("导出成功！");
			return j;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("入库打码到处失败！失败信息：" + e.getMessage());
		}
		return j;
	}

	/**
	 * 扫码过机页面
	 *
	 * @return
	 */
	@RequiresPermissions("warehouse:warehouseReceipt:code")
	@RequestMapping(value = "code")
	public String code(WarehouseReceipt warehouseReceipt, HttpServletRequest request) {
		warehouseReceipt = warehousingFacade.getWarehouseReceipt(warehouseReceipt);
		request.setAttribute("wr", warehouseReceipt);
		return "supplychain/warehouseReceipt/passingMachineView";
	}

	// @ResponseBody
	// @RequiresPermissions(" warehouse:warehouseReceipt:saveCode")
	// @RequestMapping(value = "saveCode")
	// public Map<String, Object> saveCode(Page<WarehouseReceipt> page, WarehouseReceipt warehouseReceipt,
	// HttpServletRequest request, HttpServletResponse response) {
	//
	// Map<String, Object> map = new HashMap<String, Object>();
	// try {
	// warehousingFacade.save(warehouseReceipt);
	// map.put("message", 0);
	// } catch (Throwable e) {
	// logger.error("save is error.", e);
	// map.put("message", 1);
	// map.put("data", e.getMessage());
	//
	// }
	// return map;
	// }

	// @ResponseBody
	// @RequiresPermissions("warehouse:warehouseReceipt:createPur")
	// @RequestMapping(value = "createPur")
	// public Map<String, Object> createPur(Page<WarehouseReceipt> page, WarehouseReceipt warehouseReceipt,
	// HttpServletRequest request, HttpServletResponse response) {
	// Map<String, Object> map = new HashMap<String, Object>();
	// try {
	// warehousingFacade.save(warehouseReceipt);
	// map.put("message", 0);
	// } catch (Throwable e) {
	// logger.error("save is error.", e);
	// map.put("message", 1);
	// map.put("data", e.getMessage());
	//
	// }
	// return map;
	// }

	@RequiresPermissions("warehouse:warehouseReceipt:createView")
	@RequestMapping(value = "createView")
	public String createView(WarehouseReceipt warehouseReceipt, String purchaseOrderId, HttpServletRequest request) {
		warehouseReceipt = warehousingFacade.getWarehouseReceipt(warehouseReceipt);
		request.setAttribute("wr", warehouseReceipt);
		//
		// // 获取
		// PurchaseOrder purchaseOrder = new PurchaseOrder();
		// purchaseOrder.setEvidenceId(warehouseReceipt.getEvidenceId());
		// List<PurchaseOrder> purchaseOrderList = purchaseOrderService.findList(purchaseOrder);
		// if (!CollectionUtils.isEmpty(purchaseOrderList) && purchaseOrderList.size() >= 1) {
		// // 查询采购单明细
		// PurchaseOrderItem purchaseOrderItem = new PurchaseOrderItem();
		// purchaseOrderItem.setPurchaseOrderId(purchaseOrderList.get(0).getId());
		// List<PurchaseOrderItem> itemList = purchaseOrderItemService.findList(purchaseOrderItem);
		// request.setAttribute("itemList", itemList);
		// }
		//
		// // 查询同仓库下的sku
		// Sku sku = new Sku();
		// sku.setDepartmentId(UserUtils.getUser().getOffice().getId());
		// List<Sku> skuList = skuService.findList(sku);
		// request.setAttribute("skuList", skuList);
		request.setAttribute("purchaseOrderId", purchaseOrderId);
		return "supplychain/warehouseReceipt/createView";
	}

	@RequestMapping(value = "findPurchaseOrderItem")
	@ResponseBody
	public List<PurchaseOrderItemVo> findPurchaseOrderItem(String purchaseOrderId) {
		return purchaseOrderItemService.findPurchaseOrderItemList(purchaseOrderId);
	}

	/**
	 * 扫码过机页面
	 *
	 * @return
	 */
	@RequiresPermissions("warehouse:warehouseReceipt:view")
	@RequestMapping(value = "view")
	public String view(WarehouseReceipt warehouseReceipt, HttpServletRequest request) {
		warehouseReceipt = warehousingFacade.getWarehouseReceipt(warehouseReceipt);
		request.setAttribute("wr", warehouseReceipt);
		//
		// // 获取
		//
		// PurchaseOrderItemActual purchaseOrderItemActual = new PurchaseOrderItemActual();
		// purchaseOrderItemActual.setWarehouseReceiptId(warehouseReceipt.getEvidenceId());
		// List<PurchaseOrderItemActual> itemList = purchaseOrderItemActualService.findList(purchaseOrderItemActual);
		// request.setAttribute("itemList", itemList);
		//
		// // 查询同仓库下的sku
		// Sku sku = new Sku();
		// // sku.setDepartmentId(UserUtils.getUser().getOffice().getId());
		// List<Sku> skuList = skuService.findList(sku);
		// request.setAttribute("skuList", skuList);

		return "supplychain/warehouseReceipt/view";
	}

	@RequestMapping(value = "findPurchaseOrderActualItem")
	@ResponseBody
	public List<PurchaseOrderItemActualVo> findPurchaseOrderActualItem(String evidenceId) {
		return purchaseOrderItemActualService.findPurchaseOrderItemActualList(evidenceId);
	}

	/**
	 * 过机代码
	 * @param request
	 * @param warehouseReceipt
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "passingMachine")
	public Map<String, Object> passingMachine(HttpServletRequest request, WarehouseReceipt warehouseReceipt,
			@RequestParam(value = "ids", required = false) List<String> ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			warehouseReceipt.setResourceIds(ids);
			warehousingFacade.confirm(warehouseReceipt);
			map.put("message", "0");
		} catch (Throwable e) {
			map.put("message", "1");
			map.put("data", e.getMessage());
			logger.error("Controller Error StockoutController -> passingMachine   " + e.getMessage());
		}
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "findWarehouseReceiptType")
	public JSONArray findWarehouseReceiptType() {
		return EvidenceType.getReceiptType();
	}

}
